import { defineStore } from 'pinia'
import { login as loginApi } from '@/api/system/auth.ts'

interface UserState {
  token?: string,
  name?: string,
  userInfo?: any,
  ext?: any
}

const useUserStore = defineStore('app-user', {
  state: (): UserState => {
    return {
      token: '',
      name: '张三',
      userInfo: {},
      ext: {}
    }
  },
  actions: {
    setToken(token: string) {
      this.token = token
    },
    login(data: any) {
      loginApi(data).then(res => {
        console.log('===>pinia ', res)
        this.setToken(res.data)
      })
    }
  },
  getters: {
    getToken(): any {
      return this.name || window.localStorage.getItem('name')
    }
  },
  persist: {
    storage: localStorage,
    key: 'app-user'
  }
})

export default useUserStore
